/*********************************************************************************
 *      Copyright:  (C) 2022 Zhang xiangyun<1327295677@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  get_conf.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(07/07/22)
 *         Author:  Zhang xiangyun <1327295677@qq.com>
 *      ChangeLog:  1, Release initial version on "07/07/22 16:14:36"
 *                 
 ********************************************************************************/
#include "dictionary.h"
#include "iniparser.h"
#include "mqtt.h"
#include "logger.h"
#include "get_conf.h"

#include <stdio.h>
#include <string.h>

//#define PATH "/home/zhangxiangyun/zxy/mqtt/client/src"
int get_conf(char *ini_path,mosquitto_t *mos_msg,int option);
int get_conf(char *ini_path,mosquitto_t *mos_msg,int option)
{
	dictionary       *ini=NULL;
	const char       *host;
	const char       *clientid;
	const char       *username;
	const char       *password;
	const char       *topic;

	if(NULL ==(ini=iniparser_load(ini_path)))
	{
		log_error("iniparser_load() failure!\n");
		return -1;
	}

	if(ALI==option)
	{
		host        =iniparser_getstring(ini,"aliyun:host","NULL");
		clientid    =iniparser_getstring(ini,"aliyun:clientid","NULL");
		username    =iniparser_getstring(ini,"aliyun:username","NULL");
		topic       =iniparser_getstring(ini,"aliyun:topic","NULL");
		password    =iniparser_getstring(ini,"aliyun:password","NULL");

  
		strncpy(mos_msg->host,host,SIZE);
		strncpy(mos_msg->clientid,clientid,SIZE);
		strncpy(mos_msg->username,username,SIZE);
		strncpy(mos_msg->topic,topic,SIZE);
		strncpy(mos_msg->password,password,SIZE);

		log_info("write ini massage into struct!\n");
		return 0;
	}

	if(TENCENT==option)
	{
		host        =iniparser_getstring(ini,"tencent:host","NULL");
		clientid    =iniparser_getstring(ini,"tencent:clientid","NULL");
		username    =iniparser_getstring(ini,"tencent:username","NULL");
		topic       =iniparser_getstring(ini,"tencent:topic","NULL");
		password    =iniparser_getstring(ini,"tencent:password","NULL");
  
		strncpy(mos_msg->host,host,SIZE);
		strncpy(mos_msg->clientid,clientid,SIZE);
		strncpy(mos_msg->username,username,SIZE);
		strncpy(mos_msg->topic,topic,SIZE);
		strncpy(mos_msg->password,password,SIZE);

		log_info("write ini massage into struct!\n");
		return 0;
	}

	if(HUAWEI==option)
	{
		host        =iniparser_getstring(ini,"huawei:host","NULL");
		clientid    =iniparser_getstring(ini,"huawei:clientid","NULL");
		username    =iniparser_getstring(ini,"huawei:username","NULL");
		topic       =iniparser_getstring(ini,"huawei:topic","NULL");
		password    =iniparser_getstring(ini,"huawei:password","NULL");
 
		//printf("username:%s\n",username);
		strncpy(mos_msg->host,host,SIZE);
		strncpy(mos_msg->clientid,clientid,SIZE);
		strncpy(mos_msg->username,username,SIZE);
		strncpy(mos_msg->topic,topic,SIZE);
		strncpy(mos_msg->password,password,SIZE);

		//printf("username:%s\n",mos_msg->username);
		//printf("clientid:%s\n",mos_msg->clientid);
		//printf("password:%s\n",mos_msg->password);
		//printf("topic:%s\n",mos_msg->topic);
    	//printf("host:%s\n",mos_msg->host);
		
		log_info("write ini massage into struct!\n");
		return 0;
	}

}

